package com.saifan.wyy_ov.ui.guide;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import com.saifan.wyy_ov.ui.set.ServerSetActivity;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.ui.view.CustomIndicator;

import java.util.ArrayList;
import java.util.List;

import lomasky.ma.utils.SharedPrefUtil;

/**
 * @author Wangli
 * @Description: 引导界面
 * @date: 2015/7/20 13:58
 * @version: V1.0
 */

public class GuideActivity extends AppCompatActivity {
    private android.support.v4.view.ViewPager viewpager;
    private com.saifan.wyy_ov.ui.view.CustomIndicator indicator;
    private List<View> views;

    int[] guid_imgs ={R.drawable.guide_1,R.drawable.guide_2,R.drawable.guide_3,R.drawable.guide_4};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ////设置无标题
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        this.indicator = (CustomIndicator) findViewById(R.id.indicator);
        this.viewpager = (ViewPager) findViewById(R.id.view_pager);
        views = new ArrayList<View>();
        for (int i = 0; i < 4; i++) {
            View view = getLayoutInflater().inflate(R.layout.guide_item, null);
            ImageView img = (ImageView) view.findViewById(R.id.guide_img);
            int id = guid_imgs[i];
            img.setImageResource(id);
            if (i == 3) {
                view.findViewById(R.id.guide_button).setVisibility(View.VISIBLE);
                view.findViewById(R.id.guide_button).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {


                        if (SharedPrefUtil.getBoolean(GuideActivity.this, "firstInstall", true)){
                            SharedPrefUtil.putBoolean(GuideActivity.this, "firstInstall", false);
                            Intent intent = new Intent(GuideActivity.this, ServerSetActivity.class);
                            startActivity(intent);

                        }
                        GuideActivity.this.finish();
                    }
                });
            }
            views.add(view);

        }
        viewpager.setAdapter(new MyPagerAdapter());
        this.indicator.setViewPager(viewpager);
        this.indicator.setOnPageChangeListener(new CustomIndicator.PageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 3)
                    indicator.setVisibility(View.GONE);
                else
                    indicator.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    class MyPagerAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//			super.destroyItem(container, position, object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View child = views.get(position);
            if(child.getParent()!=null){
                ((ViewPager)container).removeView(child);
            }
            container.addView(views.get(position), 0);
            return views.get(position);
        }
    }
}
